/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import beans.Identificacao;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;

/**
 *
 * @author arthur
 */
public class Cadastro extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) {
        doPost(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) {
        try {
            String url = null;
            Identificacao identificacao = new Identificacao();
            identificacao.setEmail(request.getParameter("email"));
            identificacao.setSenha(request.getParameter("senha"));

            String fileLocation = getServletContext().getRealPath("/dados/login.obj");

            List<Identificacao> users = readList(fileLocation);
            if (users != null) {
                //checa se o usuario ja esta cadastrado
                if (userExists(fileLocation, identificacao.getEmail())) {
                    JOptionPane.showMessageDialog(null, "Email ja cadastrado!");
                    url = "/cadastro.jsp";
                } else {
                    users.add(identificacao);
                    writeList(fileLocation, users);
                    JOptionPane.showMessageDialog(null, "Cadastro realizado com sucesso!");
                    url = "/cadastro.jsp";
                }
            } else {
                users = new ArrayList<>();
                users.add(identificacao);
                writeList(fileLocation, users);
                JOptionPane.showMessageDialog(null, "Arquivo criado e Cadastro realizado com sucesso!");
                url = "/cadastro.jsp";
            }

            RequestDispatcher dispatcher = request.getRequestDispatcher(url);
            dispatcher.forward(request, response);

        } catch (ServletException | IOException ex) {
        }
    }

    public boolean userExists(String fileLocation, String email) {
        List<Identificacao> users = readList(fileLocation);
        if (users != null) {
            for (int i = 0; i < users.size(); i++) {
                if (users.get(i).getEmail().equals(email)) {
                    return true;
                }
            }
        }
        return false;
    }

    public List<Identificacao> readList(String fileLocation) {
        List<Identificacao> users = new ArrayList<>();
        ObjectInputStream objIn = null;
        try {
            objIn = new ObjectInputStream(new FileInputStream(fileLocation));
            //put into arrayList
            users = (List<Identificacao>) objIn.readObject();
        } catch (IOException e) {
        } catch (ClassNotFoundException e) {
            JOptionPane.showMessageDialog(null, "Class not found");
        } finally {
            if (objIn != null) {
                try {
                    objIn.close();
                    return users;
                } catch (IOException e) {
                }
            }
        }
        return null;
    }

    public void writeList(String fileLocation, List<Identificacao> dataToWrite) {
        List<Identificacao> users = dataToWrite;
        ObjectOutputStream objOut = null;
        try {
            FileOutputStream out = new FileOutputStream(fileLocation);
            objOut = new ObjectOutputStream(out);
            objOut.writeObject(users);
        } catch (IOException e) {
        } finally {
            if (objOut != null) {
                try {
                    objOut.close();
                } catch (IOException e) {
                }
            }
        }
    }
}
